package com.self.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.self.reggie.common.BaseContext;
import com.self.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 配置拦截器,防止没有登录直接访问敏感页面
 */
@WebFilter
@Slf4j
public class LoginCheckFilter implements Filter{
    //配置路径匹配器
    public static final AntPathMatcher PATH_MATCHER=new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;

        /*1.获取本次请求的URI*/
        String requestURI = request.getRequestURI();

        log.info("拦截到的请求:{}",requestURI);

        //定义不拦截的请求路径
        String[] urls=new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/login",
                "/user/sendMsg"
        };

        /*2.判断本次请求是否需要进行拦截*/
        boolean check = check(urls, requestURI);//方法定义在下面

        /*3.如果不需要拦截,直接放行*/
        if (check){
            log.info("本次请求{}没有进行拦截",requestURI);
            filterChain.doFilter(request,response);
            return;
        }

        /*4.1判断pc端登录状态,如果登陆了直接放行*/
        if (request.getSession().getAttribute("employee")!=null){
            log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("employee"));

            //用用户的id存入线程中
            Long employeeId = (Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(employeeId);
            log.info("线程获取的id是:{}",employeeId);

            filterChain.doFilter(request,response);
            return;
        }

        /*4.2判断移动端登录状态,如果登陆了直接放行*/
        if (request.getSession().getAttribute("user")!=null){
            log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("user"));

            //用用户的id存入线程中
            Long userId = (Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);
            log.info("线程获取的id是:{}",userId);

            filterChain.doFilter(request,response);
            return;
        }


        log.info("用户未登录");
        /*5.如果未登录,使用输出流相应数据,相应的数据参考前端的拦截器*/
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        //上面的NOTLOGIN是看了前端拦截器的内容
        return;
    }

    /**
     * 判断路径是否需要拦截
     * @param urls
     * @param requestURI
     * @return
     */
    public boolean check(String[] urls,String requestURI){
        for (String url:urls){
            boolean match = PATH_MATCHER.match(url, requestURI);
            if (match){
                return true;
            }
        }
        return false;
    }
}
